﻿namespace IBFutures.Modules.Log
{
    using IBTrader.Modules;
    using NLog;
    using System;
    using System.Diagnostics;

    public static class Memory
    {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();
        private static readonly PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes");
        private static int lastMinutes;
        private static float lastAvailableMemory;
        public static float AvailableMemory
        {
            get 
            {
                if (lastAvailableMemory == 0)
                {
                    Update();
                }
                else
                {
                    W.Q(logger, Update);
                }
                return lastAvailableMemory;
            }
        }
        private static void Update()
        {
            var totalMinutes = (int)DateTime.Now.TimeOfDay.TotalMinutes;
            if (lastMinutes != totalMinutes)
            {
                lastAvailableMemory = counter.NextValue();
                lastMinutes = totalMinutes;
                logger.Debug("LastMinutes {0}, lastAvailableMemory {1}", lastMinutes, lastAvailableMemory);
            }
        }
    }
}